<?php

/**
 * @copyright       : FOSP
 * @version         : 20120323
 * @todo            : Fixture controller
 * @name            : FixtureController
 * @author          : QuangTM
 */
class FixtureController extends Zend_Controller_Action
{
    public function indexAction ()
    {
        // Get default params
        $group    = NULL;
        $leagueId = NULL;
        $unixTime = strtotime (date ('Y/m/d'));

        // Get models' instance
        $matchModel  = Match::getInstance ();
        $leagueModel = League::getInstance ();

        // Get list league 
        $groups = array (1001, 2001);
        $arrListLeagueQT = $leagueModel->getListLeagueByGroup ($groups[0]);
        $arrListLeagueTN = $leagueModel->getListLeagueByGroup ($groups[1]);

        // Get list matches' id by happen time
        $arrMatchIDs = $matchModel->getMatchIDsHappenTime ($unixTime, $leagueId);

        // Get list matches' detail
        $arrMatchDetails = $matchModel->getMatchDetailByIDs ($arrMatchIDs);

        $fixture = array ();
        foreach ($arrMatchDetails as $match)
        {
            if (!isset ($fixture[$match['Group']]))
            {
                $fixture[$match['Group']] = array (
                    $match['LeagueID'] => array (
                        'Name'       => $match['LeagueName'],
                        'SeasonName' => $match['SeasonName'],
                        'LeagueID'   => $match['LeagueID'],
                        'SeasonID'   => $match['SeasonID'],
                        'arrMatches' => array ($match)
                    )
                );
            }
            else
            {
                if (isset ($fixture[$match['Group']][$match['LeagueID']]))
                {
                    array_push ($fixture[$match['Group']][$match['LeagueID']]['arrMatches'], $match);
                }
                else
                {
                    $fixture[$match['Group']][$match['LeagueID']] = array (
                        'Name'       => $match['LeagueName'],
                        'SeasonName' => $match['SeasonName'],
                        'LeagueID'   => $match['LeagueID'],
                        'SeasonID'   => $match['SeasonID'],
                        'arrMatches' => array ($match)
                    );
                }
            }
        }
        
        // Assign to view
        $this->view->assign (array (
            'group'           => $group,
            'currTime'        => time(),
            'leagueId'        => intval($leagueId),
            'prevUnixTime'    => $unixTime - 86400,
            'currUnixTime'    => $unixTime,
            'nextUnixTime'    => $unixTime + 86400,
            'fixture'         => $fixture,
            'groups'          => $groups,
            'arrListLeagueQT' => $arrListLeagueQT,
            'arrListLeagueTN' => $arrListLeagueTN,
            'categoryID'      => CATE_ID_FIXTURE
        ));
        
        //Prepend CSS
        $this->view->headLink()->prependStylesheet($this->view->config['css'] . '/jquery-ui-1.8.18.custom.css');
        //Append JS
        $this->view->headScript()->appendFile($this->view->config['js'] . '/scrolltop.js')
            ->appendFile($this->view->config['js'] . '/jquery-ui-1.8.18.custom.min.js')
            ->appendFile($this->view->config['js'] . '/fixture.js');
        $this->view->headTitle('Lịch thi đấu bóng đá trong nước và quốc tế - VnExpress');
        $this->view->headMeta()->setName('description', 'Lịch thi đấu bóng đá Anh,Việt Nam,Cúp C1,Tây ban Nha,Bóng đá Ý 2012 cập nhật liên tục.');
        $this->view->headMeta()->setName('keywords', 'Lich thi dau,bong da,2012');
    }

    public function chitietAction ()
    {
        // Disable render layout
        $this->_helper->layout->disableLayout (true);
        $this->_helper->viewRenderer->setNoRender (true);

        $response = array (
            'error' => 0,
            'msg'   => 'Success',
            'html'  => NULL,
        );

        $date     = $this->_request->getParam ('ngay', NULL);
        $leagueId = intval ($this->_request->getParam ('lid', 0));
        $group    = $this->_request->getParam ('g', NULL);

        // Determine group
        if ($group != 'quoc-te' && $group != 'trong-nuoc')
            $group = NULL;

        if (!$leagueId)
            $leagueId = NULL;

        // Determine date to get fixture
        if (empty ($date))
            $unixTime = strtotime (date ('Y/m/d'));
        else
            $unixTime = strtotime ($date);

        // Get models' instance
        $matchModel  = Match::getInstance ();
        $leagueModel = League::getInstance ();

        // Get list league 
        // Quoc te
        $arrListLeagueQT = $arrListLeagueTN = NULL;
        $groups          = array (1001, 2001);
        if ($group == NULL || $group == 'quoc-te')
            $arrListLeagueQT = $leagueModel->getListLeagueByGroup (1001);
        if ($group == NULL || $group == 'trong-nuoc')
            $arrListLeagueTN = $leagueModel->getListLeagueByGroup (2001);

        // Get list matches' id by happen time
        $arrMatchIDs = $matchModel->getMatchIDsHappenTime ($unixTime, $leagueId);

        // Get list matches' detail
        $arrMatchDetails = $matchModel->getMatchDetailByIDs ($arrMatchIDs);

        $fixture = array ();
        foreach ($arrMatchDetails as $match)
        {
            if (!isset ($fixture[$match['Group']]))
            {
                $fixture[$match['Group']] = array (
                    $match['LeagueID'] => array (
                        'Name'       => $match['LeagueName'],
                        'SeasonName' => $match['SeasonName'],
                        'LeagueID'   => $match['LeagueID'],
                        'SeasonID'   => $match['SeasonID'],
                        'arrMatches' => array ($match)
                    )
                );
            }
            else
            {
                if (isset ($fixture[$match['Group']][$match['LeagueID']]))
                {
                    array_push ($fixture[$match['Group']][$match['LeagueID']]['arrMatches'], $match);
                }
                else
                {
                    $fixture[$match['Group']][$match['LeagueID']] = array (
                        'Name'       => $match['LeagueName'],
                        'SeasonName' => $match['SeasonName'],
                        'LeagueID'   => $match['LeagueID'],
                        'SeasonID'   => $match['SeasonID'],
                        'arrMatches' => array ($match)
                    );
                }
            }
        }

        // Assign to view
        $this->view->assign (array (
            'group'           => $group,
            'currTime'        => time (),
            'leagueId'        => intval ($leagueId),
            'prevUnixTime'    => $unixTime - 86400,
            'currUnixTime'    => $unixTime,
            'nextUnixTime'    => $unixTime + 86400,
            'fixture'         => $fixture,
            'groups'          => $groups,
            'arrListLeagueQT' => $arrListLeagueQT,
            'arrListLeagueTN' => $arrListLeagueTN,
        ));
        
        $response['html'] = $this->view->render('fixture/chitiet.phtml');
        echo Zend_Json::encode($response);
        exit;
    }

}